package fsmsim.graphicUI;


import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.DefaultListModel;
import fsmsim.dataStructure.FSM;
import fsmsim.buffer.Buffer;
import fsmsim.dataStructure.Container;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.util.Vector;


/**
 * <p>Title: FSMSim</p>
 *
 * <p>Description: Simulatore di macchine a stati finiti.</p>
 *
 * <p>Copyright: Copyright (c) 2008</p>
 *
 * <p>Company: Bollati, Donati, Gabrielli, Peli</p>
 *
 * @author Bollati, Donati, Gabrielli, Peli
 * @version 3.0
 */


public class FSMSimGUI extends javax.swing.JFrame implements FSMSimInterface {

    // Time to wait before a new simulation step
    private int DELAY = 700;

    private int sceltaAttuale;
    private boolean rapida = true;
    private String fileURL = null;

    
    /** Creates new form FSMSimGUI */
    public FSMSimGUI() {
        try {
            UIManager.setLookAndFeel(
                  "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
            //UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        }

        initComponents();

        /**
         * Set forms and dialog size
         */
        setSize(600, 450);
        dlgAbout.setSize(600, 220);
        dlgScelta.setSize(500, 240);
        dlgNuova.setSize(500, 400);
        dlgMessage.setSize(400, 160);
        
      
        this.setVisible(true);
        
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
    private void initComponents() {
        dlgAbout = new javax.swing.JDialog();
        jPanel4 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        btAboutOK = new javax.swing.JButton();
        dlgScelta = new javax.swing.JDialog();
        jPanel5 = new javax.swing.JPanel();
        btSceltaOK = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        lsSceltaOpzioni = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        dlgNuova = new javax.swing.JDialog();
        jPanel6 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        edNuovaFile = new javax.swing.JTextField();
        btNuovaCerca = new javax.swing.JButton();
        jPanel7 = new javax.swing.JPanel();
        cbNuovaRapida = new javax.swing.JRadioButton();
        cbNuovaPassoPasso = new javax.swing.JRadioButton();
        jLabel7 = new javax.swing.JLabel();
        edNuovaVelocita = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        btNuovaInizia = new javax.swing.JButton();
        btNuovaAnnulla = new javax.swing.JButton();
        fcApri = new javax.swing.JFileChooser();
        dlgMessage = new javax.swing.JDialog();
        btMessageOK = new javax.swing.JButton();
        jScrollPane7 = new javax.swing.JScrollPane();
        tpMessageText = new javax.swing.JTextPane();
        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        tpConsole = new javax.swing.JTextPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        lsFSM1 = new javax.swing.JList();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane5 = new javax.swing.JScrollPane();
        tpEventiEsterni = new javax.swing.JTextPane();
        jScrollPane6 = new javax.swing.JScrollPane();
        tpBuffer = new javax.swing.JTextPane();
        jScrollPane3 = new javax.swing.JScrollPane();
        lsFSM2 = new javax.swing.JList();
        jToolBar1 = new javax.swing.JToolBar();
        btNuova = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JSeparator();
        btRapida = new javax.swing.JToggleButton();
        btPassoPasso = new javax.swing.JToggleButton();
        jSeparator4 = new javax.swing.JSeparator();
        btStartPause = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        lbStatusText = new javax.swing.JLabel();
        jMenuBar2 = new javax.swing.JMenuBar();
        jMenu2 = new javax.swing.JMenu();
        miNuova = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        miExit = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();
        cbmiRapida = new javax.swing.JCheckBoxMenuItem();
        cbmiPassoPasso = new javax.swing.JCheckBoxMenuItem();
        jMenu3 = new javax.swing.JMenu();
        miAbout = new javax.swing.JMenuItem();

        dlgAbout.setTitle("About FSMSim");
        dlgAbout.setName("dlgAbout");
        dlgAbout.setResizable(false);
        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null,
                "FSMSim", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
                javax.swing.border.TitledBorder.DEFAULT_POSITION,
                new java.awt.Font("Tahoma", 1, 11)));
    
       
        btAboutOK.setText("OK");
        btAboutOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAboutOKActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel4Layout = new org.jdesktop.layout.
                GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
                jPanel4Layout.createParallelGroup(org.jdesktop.layout.
                                                  GroupLayout.LEADING)
                .add(jPanel4Layout.createSequentialGroup()
                     .addContainerGap()
                     .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.
                GroupLayout.LEADING)
                          .add(jPanel4Layout.createParallelGroup(org.jdesktop.
                layout.GroupLayout.LEADING)
                               .add(jPanel4Layout.createSequentialGroup()
                                    .add(jPanel4Layout.createParallelGroup(org.
                jdesktop.layout.GroupLayout.LEADING)
                                         .add(jLabel3)
                                         .add(jLabel2))
                                    .addContainerGap())
                               .add(jPanel4Layout.createSequentialGroup()
                                    .add(jLabel4,
                                         org.jdesktop.layout.GroupLayout.
                                         DEFAULT_SIZE,
                                         org.jdesktop.layout.GroupLayout.
                                         DEFAULT_SIZE, Short.MAX_VALUE)
                                    .add(9, 9, 9))
                               .add(jPanel4Layout.createSequentialGroup()
                                    .add(jLabel5)
                                    .addContainerGap(241, Short.MAX_VALUE)))
                          .add(org.jdesktop.layout.GroupLayout.TRAILING,
                               jPanel4Layout.createSequentialGroup()
                               .add(216, 216, 216)
                               .add(btAboutOK)
                               .addContainerGap())))
                );
        jPanel4Layout.setVerticalGroup(
                jPanel4Layout.createParallelGroup(org.jdesktop.layout.
                                                  GroupLayout.LEADING)
                .add(jPanel4Layout.createSequentialGroup()
                     .add(jLabel3)
                     .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                     .add(jLabel2)
                     .add(17, 17, 17)
                     .add(jLabel5)
                     .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                     .add(jLabel4)
                     .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED,
                                      org.jdesktop.layout.GroupLayout.
                                      DEFAULT_SIZE, Short.MAX_VALUE)
                     .add(btAboutOK)
                     .addContainerGap())
                );

        org.jdesktop.layout.GroupLayout dlgAboutLayout = new org.jdesktop.
                layout.GroupLayout(dlgAbout.getContentPane());
        dlgAbout.getContentPane().setLayout(dlgAboutLayout);
        dlgAboutLayout.setHorizontalGroup(
                dlgAboutLayout.createParallelGroup(org.jdesktop.layout.
                GroupLayout.LEADING)
                .add(dlgAboutLayout.createSequentialGroup()
                     .addContainerGap()
                     .add(jPanel4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                          org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                          Short.MAX_VALUE)
                     .addContainerGap())
                );
        dlgAboutLayout.setVerticalGroup(
                dlgAboutLayout.createParallelGroup(org.jdesktop.layout.
                GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING,
                     dlgAboutLayout.createSequentialGroup()
                     .addContainerGap()
                     .add(jPanel4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                          org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                          Short.MAX_VALUE)
                     .addContainerGap())
                );
        dlgScelta.setDefaultCloseOperation(javax.swing.WindowConstants.
                                           DO_NOTHING_ON_CLOSE);
        dlgScelta.setTitle("Risolvere non determinismo");
        dlgScelta.setModal(true);
        btSceltaOK.setText("OK");

        lsSceltaOpzioni.setSelectionMode(javax.swing.ListSelectionModel.
                                         SINGLE_SELECTION);
        jScrollPane4.setViewportView(lsSceltaOpzioni);

        jLabel1.setText("Opzioni disponibili");

        org.jdesktop.layout.GroupLayout jPanel5Layout = new org.jdesktop.layout.
                GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
                jPanel5Layout.createParallelGroup(org.jdesktop.layout.
                                                  GroupLayout.LEADING)
                .add(jPanel5Layout.createSequentialGroup()
                     .addContainerGap()
                     .add(jLabel1)
                     .addContainerGap(285, Short.MAX_VALUE))
                .add(org.jdesktop.layout.GroupLayout.TRAILING,
                     jPanel5Layout.createSequentialGroup()
                     .addContainerGap(323, Short.MAX_VALUE)
                     .add(btSceltaOK)
                     .addContainerGap())
                .add(jScrollPane4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                     380, Short.MAX_VALUE)
                );
        jPanel5Layout.setVerticalGroup(
                jPanel5Layout.createParallelGroup(org.jdesktop.layout.
                                                  GroupLayout.LEADING)
                .add(jPanel5Layout.createSequentialGroup()
                     .add(jLabel1)
                     .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                     .add(jScrollPane4,
                          org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 228,
                          Short.MAX_VALUE)
                     .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                     .add(btSceltaOK))
                );

        org.jdesktop.layout.GroupLayout dlgSceltaLayout = new org.jdesktop.
                layout.GroupLayout(dlgScelta.getContentPane());
        dlgScelta.getContentPane().setLayout(dlgSceltaLayout);
        dlgSceltaLayout.setHorizontalGroup(
                dlgSceltaLayout.createParallelGroup(org.jdesktop.layout.
                GroupLayout.LEADING)
                .add(dlgSceltaLayout.createSequentialGroup()
                     .addContainerGap()
                     .add(jPanel5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                          org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                          Short.MAX_VALUE)
                     .addContainerGap())
                );
        dlgSceltaLayout.setVerticalGroup(
                dlgSceltaLayout.createParallelGroup(org.jdesktop.layout.
                GroupLayout.LEADING)
                .add(dlgSceltaLayout.createSequentialGroup()
                     .addContainerGap()
                     .add(jPanel5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                          org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                          Short.MAX_VALUE)
                     .addContainerGap())
                );
        dlgNuova.setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        dlgNuova.setTitle("Nuova simulazione");
        dlgNuova.setModal(true);
        dlgNuova.setResizable(false);
        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder(
                "Definizione sistema"));
        jLabel6.setText("File:");

        btNuovaCerca.setText("Cerca...");
        btNuovaCerca.setIcon(new ImageIcon("pixmaps/changedir.gif"));

        org.jdesktop.layout.GroupLayout jPanel6Layout = new org.jdesktop.layout.
                GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
                jPanel6Layout.createParallelGroup(org.jdesktop.layout.
                                                  GroupLayout.LEADING)
                .add(jPanel6Layout.createSequentialGroup()
                     .addContainerGap()
                     .add(jLabel6)
                     .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                     .add(edNuovaFile,
                          org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 198,
                          Short.MAX_VALUE)
                     .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                     .add(btNuovaCerca)
                     .addContainerGap())
                );
        jPanel6Layout.setVerticalGroup(
                jPanel6Layout.createParallelGroup(org.jdesktop.layout.
                                                  GroupLayout.LEADING)
                .add(jPanel6Layout.createSequentialGroup()
                     .addContainerGap()
                     .add(jPanel6Layout.createParallelGroup(org.jdesktop.layout.
                GroupLayout.BASELINE)
                          .add(jLabel6)
                          .add(btNuovaCerca)
                          .add(edNuovaFile,
                               org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
                               org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                               org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                     .addContainerGap(17, Short.MAX_VALUE))
                );

        jPanel7.setBorder(javax.swing.BorderFactory.createTitledBorder(
                "Opzioni di simulazione"));
        cbNuovaRapida.setText("Rapida");
        cbNuovaRapida.setToolTipText(
                "Si vedr\u00e0 solamente il risultato della simulazione");
        cbNuovaRapida.setBorder(javax.swing.BorderFactory.createEmptyBorder(0,
                0, 0, 0));
        cbNuovaRapida.setMargin(new java.awt.Insets(0, 0, 0, 0));
        

        cbNuovaPassoPasso.setSelected(true);
        cbNuovaPassoPasso.setText("Passo passo");
        cbNuovaPassoPasso.setToolTipText(
                "Si vedr\u00e0 procedere la simulazione passo per passo");
        cbNuovaPassoPasso.setBorder(javax.swing.BorderFactory.createEmptyBorder(
                0, 0, 0, 0));
        cbNuovaPassoPasso.setMargin(new java.awt.Insets(0, 0, 0, 0));
        

        jLabel7.setText(
                "Velocit\u00e0 di simulazione (solo per la simulazione passo passo):");

        edNuovaVelocita.setText("1");

        jLabel8.setText("passi / sec");

        btNuovaInizia.setText("Inizia simulazione");
        

        btNuovaAnnulla.setText("Annulla");
        btNuovaAnnulla.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNuovaAnnullaActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel7Layout = new org.jdesktop.layout.
                GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
                jPanel7Layout.createParallelGroup(org.jdesktop.layout.
                                                  GroupLayout.LEADING)
                .add(jPanel7Layout.createSequentialGroup()
                     .addContainerGap()
                     .add(jPanel7Layout.createParallelGroup(org.jdesktop.layout.
                GroupLayout.LEADING)
                          .add(cbNuovaRapida)
                          .add(cbNuovaPassoPasso)
                          .add(jLabel7)
                          .add(jPanel7Layout.createSequentialGroup()
                               .add(edNuovaVelocita,
                                    org.jdesktop.layout.GroupLayout.
                                    PREFERRED_SIZE, 49,
                                    org.jdesktop.layout.GroupLayout.
                                    PREFERRED_SIZE)
                               .addPreferredGap(org.jdesktop.layout.LayoutStyle.
                                                RELATED)
                               .add(jLabel8)))
                     .add(42, 42, 42))
                .add(org.jdesktop.layout.GroupLayout.TRAILING,
                     jPanel7Layout.createSequentialGroup()
                     .addContainerGap(150, Short.MAX_VALUE)
                     .add(btNuovaAnnulla)
                     .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                     .add(btNuovaInizia)
                     .addContainerGap())
                );
        jPanel7Layout.setVerticalGroup(
                jPanel7Layout.createParallelGroup(org.jdesktop.layout.
                                                  GroupLayout.LEADING)
                .add(jPanel7Layout.createSequentialGroup()
                     .addContainerGap()
                     .add(cbNuovaRapida)
                     .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                     .add(cbNuovaPassoPasso)
                     .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                     .add(jLabel7)
                     .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                     .add(jPanel7Layout.createParallelGroup(org.jdesktop.layout.
                GroupLayout.BASELINE)
                          .add(edNuovaVelocita,
                               org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
                               org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                               org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                          .add(jLabel8))
                     .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED,
                                      org.jdesktop.layout.GroupLayout.
                                      DEFAULT_SIZE, Short.MAX_VALUE)
                     .add(jPanel7Layout.createParallelGroup(org.jdesktop.layout.
                GroupLayout.BASELINE)
                          .add(btNuovaAnnulla)
                          .add(btNuovaInizia))
                     .addContainerGap())
                );

        org.jdesktop.layout.GroupLayout dlgNuovaLayout = new org.jdesktop.
                layout.GroupLayout(dlgNuova.getContentPane());
        dlgNuova.getContentPane().setLayout(dlgNuovaLayout);
        dlgNuovaLayout.setHorizontalGroup(
                dlgNuovaLayout.createParallelGroup(org.jdesktop.layout.
                GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING,
                     dlgNuovaLayout.createSequentialGroup()
                     .addContainerGap()
                     .add(dlgNuovaLayout.createParallelGroup(org.jdesktop.
                layout.GroupLayout.TRAILING)
                          .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel7,
                               org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                               org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                               Short.MAX_VALUE)
                          .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel6,
                               org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                               org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                               Short.MAX_VALUE))
                     .addContainerGap())
                );
        dlgNuovaLayout.setVerticalGroup(
                dlgNuovaLayout.createParallelGroup(org.jdesktop.layout.
                GroupLayout.LEADING)
                .add(dlgNuovaLayout.createSequentialGroup()
                     .addContainerGap()
                     .add(jPanel6,
                          org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
                          org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                          org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                     .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                     .add(jPanel7, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                          org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                          Short.MAX_VALUE)
                     .addContainerGap())
                );

        fcApri.setDialogTitle("Specifica del sistema");
        dlgMessage.setModal(true);
        dlgMessage.setResizable(false);
        btMessageOK.setText("OK");
        btMessageOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btMessageOKActionPerformed(evt);
            }
        });

        jScrollPane7.setOpaque(false);
        tpMessageText.setEditable(false);
        jScrollPane7.setViewportView(tpMessageText);

        org.jdesktop.layout.GroupLayout dlgErrorLayout = new org.jdesktop.
                layout.GroupLayout(dlgMessage.getContentPane());
        dlgMessage.getContentPane().setLayout(dlgErrorLayout);
        dlgErrorLayout.setHorizontalGroup(
                dlgErrorLayout.createParallelGroup(org.jdesktop.layout.
                GroupLayout.LEADING)
                .add(dlgErrorLayout.createSequentialGroup()
                     .addContainerGap()
                     .add(dlgErrorLayout.createParallelGroup(org.jdesktop.
                layout.GroupLayout.LEADING)
                          .add(org.jdesktop.layout.GroupLayout.TRAILING,
                               btMessageOK)
                          .add(jScrollPane7,
                               org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                               380, Short.MAX_VALUE))
                     .addContainerGap())
                );
        dlgErrorLayout.setVerticalGroup(
                dlgErrorLayout.createParallelGroup(org.jdesktop.layout.
                GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING,
                     dlgErrorLayout.createSequentialGroup()
                     .addContainerGap()
                     .add(jScrollPane7,
                          org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 121,
                          Short.MAX_VALUE)
                     .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                     .add(btMessageOK)
                     .addContainerGap())
                );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("FSMSim v 1.0");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setName("mainFrm");
        jSplitPane1.setDividerLocation(200);
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jSplitPane1.setPreferredSize(new java.awt.Dimension(600, 350));
        jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder(
                "Console"));
        jScrollPane1.setPreferredSize(new java.awt.Dimension(450, 200));
        jScrollPane1.setAutoscrolls(true);
        jScrollPane1.setWheelScrollingEnabled(true);
        tpConsole.setBorder(null);
        tpConsole.setFont(Font.getFont("Courier"));
        tpConsole.setEditable(false);
        jScrollPane1.setViewportView(tpConsole);

        jSplitPane1.setRightComponent(jScrollPane1);

        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1,
                javax.swing.BoxLayout.X_AXIS));

        jPanel1.setPreferredSize(new java.awt.Dimension(600, 200));
        jScrollPane2.setBorder(javax.swing.BorderFactory.createTitledBorder(
                "FSM 1"));
        jScrollPane2.setPreferredSize(new java.awt.Dimension(150, 150));
        lsFSM1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        lsFSM1.setPreferredSize(new java.awt.Dimension(150, 150));
        lsFSM1.setEnabled(false);
        jScrollPane2.setViewportView(lsFSM1);

        jPanel1.add(jScrollPane2);

        jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2,
                javax.swing.BoxLayout.Y_AXIS));

        jPanel2.setPreferredSize(new java.awt.Dimension(150, 150));
        jScrollPane5.setBorder(javax.swing.BorderFactory.createTitledBorder(
                "Eventi esterni"));
        tpEventiEsterni.setBorder(null);
        tpEventiEsterni.setPreferredSize(new java.awt.Dimension(150, 75));
        tpEventiEsterni.setEditable(false);
        jScrollPane5.setViewportView(tpEventiEsterni);

        jPanel2.add(jScrollPane5);

        jScrollPane6.setBorder(javax.swing.BorderFactory.createTitledBorder(
                "Buffer"));
        tpBuffer.setBorder(null);
        tpBuffer.setPreferredSize(new java.awt.Dimension(150, 75));
        tpBuffer.setEditable(false);
        jScrollPane6.setViewportView(tpBuffer);

        jPanel2.add(jScrollPane6);

        jPanel1.add(jPanel2);

        jScrollPane3.setBorder(javax.swing.BorderFactory.createTitledBorder(
                "FSM 2"));
        jScrollPane3.setPreferredSize(new java.awt.Dimension(150, 150));
        lsFSM2.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        lsFSM2.setPreferredSize(new java.awt.Dimension(150, 150));
        lsFSM2.setEnabled(false);
        jScrollPane3.setViewportView(lsFSM2);

        jPanel1.add(jScrollPane3);

        jSplitPane1.setLeftComponent(jPanel1);

        getContentPane().add(jSplitPane1, java.awt.BorderLayout.CENTER);

        btNuova.setIcon(new ImageIcon("pixmaps/new.gif"));
        btNuova.setToolTipText("Carica un sistema da simulare");
        btNuova.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        btNuova.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                miNuovaActionPerformed(evt);
            }
        });

        jToolBar1.add(btNuova);

        jSeparator2.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jSeparator2.setMaximumSize(new java.awt.Dimension(5, 32768));
        jToolBar1.add(jSeparator2);

        btRapida.setToolTipText("Simulazione veloce");
        btRapida.setIcon(new ImageIcon("pixmaps/rapida.gif"));
        btRapida.setSize(20, 21);
        
        jToolBar1.add(btRapida);

        btPassoPasso.setToolTipText("Simulazione lenta");
        btPassoPasso.setIcon(new ImageIcon("pixmaps/passopasso.gif"));
        btPassoPasso.setSelected(true);
        

        jToolBar1.add(btPassoPasso);

        jSeparator4.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jSeparator4.setMaximumSize(new java.awt.Dimension(3, 32768));
        jToolBar1.add(jSeparator4);

        btStartPause.setToolTipText("Avvia la simulazione");
        btStartPause.setIcon(new ImageIcon("pixmaps/avvia.gif"));
        btStartPause.setEnabled(false);
        btStartPause.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btStartPauseActionPerformed(evt);
            }
        });
        jToolBar1.add(btStartPause);

        getContentPane().add(jToolBar1, java.awt.BorderLayout.NORTH);

        jPanel3.setLayout(new java.awt.BorderLayout());

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        lbStatusText.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lbStatusText.setText("Nessuna simulazione selezionata...");
        lbStatusText.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        jPanel3.add(lbStatusText, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel3, java.awt.BorderLayout.SOUTH);

        jMenu2.setText("File");
        miNuova.setText("Nuova simulazione...");
        miNuova.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                miNuovaActionPerformed(evt);
            }
        });

        jMenu2.add(miNuova);

        jMenu2.add(jSeparator1);

        miExit.setText("Exit");
        miExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                miExitActionPerformed(evt);
            }
        });

        jMenu2.add(miExit);

        jMenuBar2.add(jMenu2);

        jMenu1.setText("Modalit\u00e0");
        cbmiRapida.setText("Rapida");
       // cbmiRapida.setSelected(true);
        
        jMenu1.add(cbmiRapida);

        cbmiPassoPasso.setText("Passo passo");
        cbmiPassoPasso.setSelected(true);
        

        jMenu1.add(cbmiPassoPasso);

        jMenuBar2.add(jMenu1);

        jMenu3.setText("?");
        miAbout.setText("About");
        miAbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                miAboutActionPerformed(evt);
            }
        });

        jMenu3.add(miAbout);
        jMenuBar2.add(jMenu3);
        setJMenuBar(jMenuBar2);

        pack();
    } // </editor-fold>

    private void btMessageOKActionPerformed(java.awt.event.ActionEvent evt) {
        dlgMessage.hide();
    }

    private void btNuovaAnnullaActionPerformed(java.awt.event.ActionEvent evt) {
        dlgNuova.hide();
    }

    private void miNuovaActionPerformed(java.awt.event.ActionEvent evt) {
        dlgNuova.show();
    }
    
    private void btAboutOKActionPerformed(java.awt.event.ActionEvent evt) {
        dlgAbout.hide();
    }

    private void miExitActionPerformed(java.awt.event.ActionEvent evt) {
        System.exit(0);
    }

    private void miAboutActionPerformed(java.awt.event.ActionEvent evt) {
        dlgAbout.show();
    }

    private void btStartPauseActionPerformed(java.awt.event.ActionEvent evt) {
        btStartPause.setEnabled(false);
        statusBarMessage("Simulazione avviata.");
    }


    // Variables declaration - do not modify
    private javax.swing.JButton btAboutOK;
    private javax.swing.JButton btMessageOK;
    private javax.swing.JButton btNuova;
    private javax.swing.JButton btNuovaCerca;
    private javax.swing.JButton btNuovaInizia;
    private javax.swing.JButton btNuovaAnnulla;
    private javax.swing.JToggleButton btPassoPasso;
    private javax.swing.JToggleButton btRapida;
    private javax.swing.JButton btSceltaOK;
    private javax.swing.JButton btStartPause;
    private javax.swing.JRadioButton cbNuovaPassoPasso;
    private javax.swing.JRadioButton cbNuovaRapida;
    private javax.swing.JCheckBoxMenuItem cbmiPassoPasso;
    private javax.swing.JCheckBoxMenuItem cbmiRapida;
    private javax.swing.JDialog dlgAbout;
    private javax.swing.JDialog dlgMessage;
    private javax.swing.JDialog dlgNuova;
    private javax.swing.JDialog dlgScelta;
    private javax.swing.JTextField edNuovaFile;
    private javax.swing.JTextField edNuovaVelocita;
    private javax.swing.JFileChooser fcApri;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JScrollPane jScrollPane7;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTextPane tpConsole;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JLabel lbStatusText;
    private javax.swing.JList lsFSM1;
    private javax.swing.JList lsFSM2;
    private javax.swing.JList lsSceltaOpzioni;
    private javax.swing.JMenuItem miAbout;
    private javax.swing.JMenuItem miExit;
    private javax.swing.JMenuItem miNuova;
    private javax.swing.JTextPane tpBuffer;
    private javax.swing.JTextPane tpMessageText;
    private javax.swing.JTextPane tpEventiEsterni;

    
    
    public int getDelay() {
        return this.DELAY;
    }
    
    public void setModality() {
        if(!cbNuovaRapida.isSelected()){
        try {
            DELAY = (int) (1000 / (Double.parseDouble(edNuovaVelocita.getText())));
        } catch (Exception e) {
            DELAY = 1000;
           
        }
        }else DELAY = 100;
    }
    
    
    public void displayStateFSM1(FSM fsm) {
        this.lsFSM1.setListData(fsm.toVector());
    }
    
    public void displayStateFSM2(FSM fsm) {
        this.lsFSM2.setListData(fsm.toVector());
    }
    
    public void displayOutputEventList(Container listaEventiEsterni) {
        this.tpEventiEsterni.setText(listaEventiEsterni.toString());
    }
    
    public void displayBuffer(Buffer buffer) {
        this.tpBuffer.setText(buffer.toString());
    }

    public String getFileUrl() {
        if (fcApri.showOpenDialog(this) != 0) {
            return "";
        }
        try {
            edNuovaFile.setText(fcApri.getSelectedFile().getCanonicalPath());
        }
        catch(IOException e) {}
        
        // check if a filename has been provided
        if (edNuovaFile.getText().equalsIgnoreCase("")) {
            return "";
        }
        // store it locally
        setFileURL(edNuovaFile.getText());

        this.cleanOutput();
               
        this.setReinit(true);
        
        statusBarMessage(
                "file caricato.");

        return this.fileURL;
    }

    /**
     * Populate ui with FSM states info's
     *
     * @param fsm1 Object[] List of states of FSM1
     * @param fsm2 Object[] List of states of FSM2
     */
    public void setFSMStates(Object[] fsm1, Object[] fsm2) {

        DefaultListModel fsm1Data = new DefaultListModel();
        DefaultListModel fsm2Data = new DefaultListModel();

        for (int i = 0; i < fsm1.length; i++) {
            fsm1Data.addElement(fsm1[i]);
        }
        lsFSM1.setModel(fsm1Data);

        for (int i = 0; i < fsm2.length; i++) {
            fsm2Data.addElement(fsm2[i]);
        }
        lsFSM2.setModel(fsm2Data);
    }
    
    
     public void update(Container listaEventiEsterni,Vector<FSM> fsms,Buffer buffer) {
        displayOutputEventList(listaEventiEsterni);
        
        //for (int i = 0 ; i < fsms.size() ; i++) 
            displayStateFSM1(fsms.get(0));
            displayStateFSM2(fsms.get(1));
        
        displayBuffer(buffer);
        
        displaySystemState(fsms, buffer.toString(),listaEventiEsterni.toString());
        consoleOut("stato FSM1: " + fsms.get(0).toString() + "        " 
                + buffer + "        stato FSM2: " + fsms.get(1).toString() + "\n");
    }


    /**
     * Print out system status.
     * Select current states from lists and put updated text in buffer
     * and external event boxes.
     *
     * @param fsm1 int Actual FSM1 status index
     * @param buffer String A rapresentation of buffer and it's content
     * @param fsm2 int Actual FSM1 status index
     * @param eventiEsterni String A rapresentation of external event list
     */
    public void displaySystemState(Vector<FSM> fsms, String buffer, String eventiEsterni) {
        //solo per le 2 macchine
        lsFSM1.setSelectedIndex(fsms.get(0).getCurrentStateIndex());
        tpBuffer.setText(buffer);
        tpEventiEsterni.setText(eventiEsterni);
        lsFSM2.setSelectedIndex(fsms.get(1).getCurrentStateIndex());
    }

    /**
     * Print text in the console
     *
     * @param text String
     */
    public void consoleOut(String text) {
        tpConsole.setText(tpConsole.getText() + text);
    }

    /**
     * Let user choose from some options.
     * Pop up a dialog with list control from which user select
     * desidered option.
     *
     * @param options String[] A list of possible choice
     * @return int Choosed item index
     */
    public int select(String[] options) {
        DefaultListModel sceltePossibili = new DefaultListModel();

        for (int i = 0; i < options.length; i++) {
            sceltePossibili.addElement(options[i]);
        }
        lsSceltaOpzioni.setModel(sceltePossibili);
        lsSceltaOpzioni.setSelectedIndex(0);
        dlgScelta.setVisible(true);

        return (sceltaAttuale == -1) ? 0 : sceltaAttuale;
    }

    /**
     * Popup a modal messages.
     *
     * @param title String Message box title
     * @param message String Message box content
     */
    public void messageBox(String title, String message) {
        tpMessageText.setText(message);
        dlgMessage.setTitle(title);
        dlgMessage.show();
    }

    public void statusBarMessage(String message) {
        lbStatusText.setText(message);
    }


    // The following funcions addEvent run's options

    private void setRapidSimulation(boolean stato) {
        rapida = stato;
    }

    public boolean getRapidSimulation() {
        return rapida;
    }

    private void setFileURL(String filename) {
        fileURL = filename;
    }

    
    /**
     * Let user choose from some options.
     * Pop up a dialog with list control from which user select
     * desidered option.
     *
     * @param options String[] A list of possible choice
     * @return int Choosed item index
     */
    public void cleanOutput() {
        // clear gui from previous data
        lsFSM1.setModel(new DefaultListModel());
        lsFSM2.setModel(new DefaultListModel());
        tpConsole.setText("");
        tpBuffer.setText("");
        tpMessageText.setText("");
        tpEventiEsterni.setText("");
    }
    
    
    
    public void setTextAbout(String title, String authors, String version) {
        jLabel2.setText(title);
        jLabel5.setText(version);
        jLabel4.setText(authors);
        jLabel3.setText("Autori:");
      }
    
    public void displayChoices(String[] options) {
        DefaultListModel sceltePossibili = new DefaultListModel();

        for (int i = 0; i < options.length; i++) {
            sceltePossibili.addElement(options[i]);
        }
        lsSceltaOpzioni.setModel(sceltePossibili);
        lsSceltaOpzioni.setSelectedIndex(0);
    }
    
    public int getChoices() {
        return this.sceltaAttuale = lsSceltaOpzioni.getSelectedIndex();
    }
    
    public void closeChoices() {
        this.dlgScelta.hide();
    }
    
    public void closeDlgNuova() {
        this.dlgNuova.setVisible(false);
    }
    
    public void setStep2StepSimulation() {
        cbNuovaRapida.setSelected(false);
        btRapida.setSelected(false);
        cbmiRapida.setSelected(false);
        cbNuovaPassoPasso.setSelected(true);
        btPassoPasso.setSelected(true);
        cbmiPassoPasso.setSelected(true);
        this.setModality();
        setRapidSimulation(btRapida.isSelected());
    }
    
    public void setRapidSimulation() {
        cbNuovaPassoPasso.setSelected(false);
        btPassoPasso.setSelected(false);
        cbmiPassoPasso.setSelected(false);
        cbNuovaRapida.setSelected(true);
        btRapida.setSelected(true);
        cbmiRapida.setSelected(true);
        this.DELAY = 100;
        setRapidSimulation(btRapida.isSelected());
    }
    
    public void setReinit(boolean status) {
         btStartPause.setEnabled(status);
    }
    
    public void addNonDeterminismListenerButton(ActionListener mal) {
        this.btSceltaOK.addActionListener(mal);
    }
    
    public void addNewChooserFileListenerButton(ActionListener mal) {
        this.btNuovaCerca.addActionListener(mal);
    }
    
    public void addSimulationListenerButton(ActionListener mal) {
        this.btStartPause.addActionListener(mal);    
    }
    
    public void addLoadSimulationButton(ActionListener mal) {
        this.btNuovaInizia.addActionListener(mal);
    }
    
    public void addStepToStepButton(ActionListener mal) {
        this.btPassoPasso.addActionListener(mal);
    }
    
    public void addRapidButton(ActionListener mal) {
        this.btRapida.addActionListener(mal);
    }
    
    public void addStepToStepComboBox(ActionListener mal) {
        this.cbNuovaPassoPasso.addActionListener(mal);
    }
    
    public void addRapidComboBox(ActionListener mal) {
        this.cbNuovaRapida.addActionListener(mal);
    }
    
    public void addStepToStepMenuItem(ActionListener mal) {
        this.cbmiPassoPasso.addActionListener(mal);
    }
    
    public void addRapidMenuItem(ActionListener mal) {
        this.cbmiRapida.addActionListener(mal);
    }


    @Override
    public void toBack() {
        super.toBack();
    }
    
   public void addAbout(ActionListener mal) {
        this.miAbout.addActionListener(mal);
    }

}
